package config

import (
	"github.com/spf13/viper"
	"strings"
	"github.com/fsnotify/fsnotify"
	"github.com/lexkong/log"
)

type Config struct {
	Name string
}

func Init(cfg string) error  {
	c := Config{
		Name:cfg,
	}

	//init config files
	if err := c.initConfig(); err != nil{
		return err
	}
	//初始化log配置
	c.initLog()
	//watch config mod && Hot-loading it
	c.watchConfig()
	return nil
}

func (c *Config) initConfig() error  {
	if c.Name != ""{
	viper.SetConfigFile(c.Name) // 如果指定了配置文件，则解析指定的配置文件
	}else {
		viper.AddConfigPath("conf")// 如果没有指定配置文件，则解析默认的配置文件
		viper.AddConfigPath("config")
	}
	viper.SetConfigType("yaml") // 设置配置文件格式为YAML
	viper.AutomaticEnv()  // 读取匹配的环境变量
	viper.SetEnvPrefix("APISERVER")  // 读取环境变量的前缀为APISERVER
	replacer := strings.NewReplacer(",","_")
	viper.SetEnvKeyReplacer(replacer)
	if err := viper.ReadInConfig();err != nil{ // viper解析配置文件
		return err
	}
	return nil
}

func (c *Config)initLog()  {
	passLagerCfg := log.PassLagerCfg {
		Writers:        viper.GetString("log.writers"),
		LoggerLevel:    viper.GetString("log.logger_level"),
		LoggerFile:     viper.GetString("log.logger_file"),
		LogFormatText:  viper.GetBool("log.log_format_text"),
		RollingPolicy:  viper.GetString("log.rollingPolicy"),
		LogRotateDate:  viper.GetInt("log.log_rotate_date"),
		LogRotateSize:  viper.GetInt("log.log_rotate_size"),
		LogBackupCount: viper.GetInt("log.log_backup_count"),
	}
	log.InitWithConfig(&passLagerCfg)
}

// 监控配置文件变化并热加载程序
func (c *Config)watchConfig()  {
	viper.WatchConfig()
	viper.OnConfigChange(func(e fsnotify.Event) {
		log.Infof("Config file changed: %s", e.Name)
	})
}